package com.mingzhi.blog.config;

import com.mingzhi.blog.enmus.ResultCodeEnum;
import com.mingzhi.blog.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

import java.net.NoRouteToHostException;

@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handler(Exception e){
        log.error("发生异常,e:",e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler
    public Result handler(UnsupportedOperationException e){
        log.error("发生异常,e:",e.getMessage());
        return Result.fail(ResultCodeEnum.PARAM_INVAILD.getCode(),e.getMessage());
    }

    @ExceptionHandler
    public Result handler(NoResourceFoundException e){
        log.error("发生异常,e:",e.getResourcePath());
        return Result.fail(ResultCodeEnum.NO_RESOURCES.getCode(),e.getMessage());
    }
}
